我想根据用户在表单上的选择更改按钮(提交)上的数据确认属性的值。我将以下内容放在下拉列表的更改功能上:
...
if($("#"+select_name).val() == "abc")
{
$(".variable_button").attr("data-confirm","abc is good choice!");
} else
{
$(".variable_button").attr("data-confirm","abc would have been great but this is fine too...");
}
...
我面临的问题是,显然数据确认一旦被分配了一个非空字符串就无法更改。我在服务器代码中将它设置为“”。并且,当用户首次在下拉列表中进行选择时,它会更改为上面显示的两条消息之一。但如果用户再次更改选择,数据确认消息不会改变。这是按设计还是我遗漏了什么?
最佳答案
不要使用.attr()
,使用.data()
:
var newData = ($("#"+select_name).val() == "abc")
? "abc is good choice!"
: "abc would have been great but this is fine too...";
$(".variable_button").data("confirm", newData);
关于javascript - 覆盖数据确认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8734002/