0

我知道这是一个小问题,但我没有找到任何解决方案,所以我有一个 javascript 变量 <script> var coords = []; </script>,我想把它放在我的视图中:

<input type="submit" name="Tracking" value="Tracking" data-coords="//here !!!"/>

所以如果有人有任何想法,我将不胜感激。

4

3 回答 3

1

If you want to set dynamic values to something in your HTML, you should use JavaScript to do it. The exact way of doing this depends on context - when exactly you want to set those values and what you want to do with them before.

If you want to set value of data-coords on DOM load and you use jQuery, write code that updates data-coords attribute of this input inside $(document).ready function. You can use attr function to do this if you use jQuery.

于 2013-04-29T09:04:18.420 回答
1

您可以在客户端处理它:

// javascript version
// safe, if you have only one element that has a name "Tracking"
var btn = document.getElementsByName("Tracking")[0].value;
var a = document.createAttribute('data-coords');
a.value = coords;
btn.setAttributeNode(a);

// or do it easier with jquery
$('[name="Tracking"]').attr('data-coords', coords);
于 2013-04-29T09:02:49.840 回答
1

方法1;您可以使用此代码;

 $("[name='Tracking']").attr("data-coords",coords);

方法2;

如果不需要使用数据坐标

你可以使用 jquery .data()..

替换这个;

<input type="submit" id="Tracking" name="Tracking" value="Tracking"/>

$("#Tracking").data("data-coords", coords);
于 2013-04-29T09:04:07.417 回答