jQuery:从数据库加载ID标签

发布于 2024-09-11 18:04:32 字数 838 浏览 1 评论 0原文

因此,我有很多名称相同但 ID 不同的单选按钮,并且我不想在页面加载时选择其中的特定一个。所需按钮的 ID 被保存到数据库中。我已经为实际的 ajax 调用尝试过这种解决方案,但可惜它不起作用。

$.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            var buttonID = "#"+data.buttonID; // data.buttonID = "button5"
            $(buttonID).attr("checked", true);
        }
});

而 HTML 部分就是这种方式,除了有更多的按钮:

<input type="radio" id="button1" name="example" value="value1"/>
<input type="radio" id="button2" name="example" value="value2"/>
<input type="radio" id="button3" name="example" value="value3"/>
<input type="radio" id="button4" name="example" value="value4"/>
<input type="radio" id="button5" name="example" value="value5"/>

So I have lots of radio buttons with same name but different ID and I wan't specific one of them to be selected on page load. The ID of desired button is saved to the database. I've tried this kind of solution for the actual ajax call, but alas it didn't work.

$.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            var buttonID = "#"+data.buttonID; // data.buttonID = "button5"
            $(buttonID).attr("checked", true);
        }
});

while the HTML part is in this sorta manner, except with a whole lot more of buttons:

<input type="radio" id="button1" name="example" value="value1"/>
<input type="radio" id="button2" name="example" value="value2"/>
<input type="radio" id="button3" name="example" value="value3"/>
<input type="radio" id="button4" name="example" value="value4"/>
<input type="radio" id="button5" name="example" value="value5"/>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-09-18 18:04:32

仔细检查你返回的 JSON,你的整体方法是正确的,你可以在这里测试它: http://jsfiddle. net/nick_craver/KuK3Z/

我敢打赌,你的 data.buttonID 与你想象的不完全一样,或者你的代码没有在里面运行document.ready,如下所示:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            var buttonID = "#"+data.buttonID; // data.buttonID = "button5"
            $(buttonID).attr("checked", true);
        }
  });
});

如果它没有在 document.ready 中运行,并且您的 AJAX 调用在元素准备就绪之前完成,则 $("# button5") 选择器找不到任何要检查的内容。或者,如果可以的话,最好在渲染页面时在正确的 内渲染 checked="checked" ,并消除完全是 AJAX 调用。

Double check your JSON coming back, your overall approach is correct, you can test it here: http://jsfiddle.net/nick_craver/KuK3Z/

I would bet that your data.buttonID isn't quite what you think it is, or your code isn't running inside a document.ready, like this:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            var buttonID = "#"+data.buttonID; // data.buttonID = "button5"
            $(buttonID).attr("checked", true);
        }
  });
});

If it's not running in a document.ready and your AJAX call finishes before your elements are ready, the $("#button5") selector won't find anything to check. Alternatively, and much better if you can could be to just render a checked="checked" inside the correct <input /> when you render the page, and eliminate the AJAX call altogether.

溇涏 2024-09-18 18:04:32

尝试检查你的数据响应:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            alert(data['buttonID']);
        }
  });
});

如果未定义,请检查你的php或我的最后一个解决方案,否则尝试:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            $("#"+data['buttonID']).attr("checked",true);
        }
  });
});

我一次将Json返回到jquery时遇到问题,返回的json字符串带有许多斜杠...
如果第一个解决方案给你一个错误,请尝试这个(这将报告从服务器返回的字符串而不解码 json):

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "text",
        data: dataString,
        success: function(data)
        {
            alert(data);
        }
  });
});

对不起我的英语。告诉我一些事情如果有效的话...

Try to check your data response:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            alert(data['buttonID']);
        }
  });
});

if is undefined check your php or my last solution, otherwise try:

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "json",
        data: dataString,
        success: function(data)
        {
            $("#"+data['buttonID']).attr("checked",true);
        }
  });
});

I've a problem once time returning the Json to jquery, the json string was returned with many slashes...
try this in case the first solution gaves to you an error (this will report the string returned form server without decoding the json):

$(function() {
  $.ajax({
        type: "POST",
        url: "load_config.php",
        dataType: "text",
        data: dataString,
        success: function(data)
        {
            alert(data);
        }
  });
});

Sorry for my english. tell me something if it worked...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文