:radio 和 :checked 没有传递给我的 var

发布于 2024-10-10 01:32:33 字数 2266 浏览 0 评论 0原文

我的页面上有几个表单,它们使用相同的函数来处理它们。一切都进展顺利,直到我想添加一个单选按钮表单来启用/禁用数据库中的记录。

我已经设置了页面,以便在元素附近有一个隐藏的表单。用户单击 div,我的脚本抓取该元素的 html 并将其放入 var 中,显示表单并将 html (var) 转储到文本区域中并隐藏该元素。进行一些更改,单击提交,表单提交,隐藏并使用新的 html(facebook 样式)重新显示元素。没问题。

当我编写代码来捕获“选中”的单选按钮时,它工作正常。但是,如果我更改为其他单选按钮,则表单不会捕获新值。这是怎么回事?

if (field == "disabled") {
            var txt = $('#disabled input[name="product_disabled"]:radio').val();
        } else {
            var txt = $(this).html();
        };

这捕获了值,没问题...

但是,在我的表单处理程序中,这仅获取上述信息,而不是表单中的新信息。

var newtxt = $('#disabled input[name="product_disabled"]:radio').val();

很奇怪...

这是完整的 jQuery

//Begin editing section - click handler on text shows the form//
$('[class^=edit_]').click(function(){
    var element = $(this).attr('class');
    var field = element.split('_')[1];
    if (field == "disabled") {
            var txt = approval;
        } else {
            var txt = $(this).html();
        };
    //var txt = $(this).html();
    //$('[class='+element+']').hide();
    $(this).hide();
    $('[id='+field+']').show();
    $('[class=product_'+field+']').val(txt);
    $('[class=product_'+field+']').focus();             
});
//Process the edit forms//
$('form').submit(function(e){
    e.preventDefault();
    alert($(this).html());
    var element = $(this).attr('id');
    var newtxt = $(this + "input[name='product_disabled']:radio").val(); //$('[class=product_'+element+']').val();
    var product = pid;
    var productImage = imageUrl;
    var rb = $('#disabled input[name="product_disabled"]:radio').val();

    $.getJSON("cfc/editProduct.cfc?method=updateText&returnformat=json", 
                {column:element, id:product, updateText:newtxt, image:productImage },
                function(response) {
                    //Handle the result
                    if(response.MESSAGE == "success") {
                        $('form').hide();
                        $('[class=edit_'+element+']').show();
                        $('[class=edit_'+element+']').html(newtxt);
                    } else {
                        alert(response.MESSAGE);
                    }
                });
});
//End editing section//

I have several forms on my page that use the same function to process them. Everything has been working just dandy until I wanted to add a radio button form to enable/disable the record in the database.

I've got the page set up so that there's a hidden form near an element. User clicks the div and my script grabs the html of the element and puts it into a var, shows the form and dumps the html (var) into a textarea and hides the element. Make some changes, click submit, form submits, hides and re-shows the element with the new html (facebook style). No problem.

When I write the code to capture the radio button that is "checked" it works fine. But, if I change to the other radio button, the form isn't capturing the new value. What's up?

if (field == "disabled") {
            var txt = $('#disabled input[name="product_disabled"]:radio').val();
        } else {
            var txt = $(this).html();
        };

This captures the value, no problem...

But, in my form handler, this just takes the above info, not the new info from the form.

var newtxt = $('#disabled input[name="product_disabled"]:radio').val();

Very strange...

Here's the full jQuery

//Begin editing section - click handler on text shows the form//
$('[class^=edit_]').click(function(){
    var element = $(this).attr('class');
    var field = element.split('_')[1];
    if (field == "disabled") {
            var txt = approval;
        } else {
            var txt = $(this).html();
        };
    //var txt = $(this).html();
    //$('[class='+element+']').hide();
    $(this).hide();
    $('[id='+field+']').show();
    $('[class=product_'+field+']').val(txt);
    $('[class=product_'+field+']').focus();             
});
//Process the edit forms//
$('form').submit(function(e){
    e.preventDefault();
    alert($(this).html());
    var element = $(this).attr('id');
    var newtxt = $(this + "input[name='product_disabled']:radio").val(); //$('[class=product_'+element+']').val();
    var product = pid;
    var productImage = imageUrl;
    var rb = $('#disabled input[name="product_disabled"]:radio').val();

    $.getJSON("cfc/editProduct.cfc?method=updateText&returnformat=json", 
                {column:element, id:product, updateText:newtxt, image:productImage },
                function(response) {
                    //Handle the result
                    if(response.MESSAGE == "success") {
                        $('form').hide();
                        $('[class=edit_'+element+']').show();
                        $('[class=edit_'+element+']').html(newtxt);
                    } else {
                        alert(response.MESSAGE);
                    }
                });
});
//End editing section//

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

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

发布评论

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

评论(1

倾城花音 2024-10-17 01:32:33

我遇到了类似的问题,其中第一个选项是值,所以我使用了这个(尽管它用于选择输入)

$(this).val($(this + "option:first").val());

I had a similar issue where the first option was empty value, so I used this ( although it was for a select input )

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