表单元素在 jquery 厚盒内表现异常

发布于 2024-08-14 19:10:58 字数 780 浏览 1 评论 0原文

我正在使用jquery的thickbox插件(版本3),在其中我想放置一个将使用$.post提交的表单。但表单元素的行为却没有达到应有的效果。例如,

<select name="priority" id="priority">
    <?php  for($i = 5; $i > 0; $i--){    ?>                                         
           <!-- <input type="radio" class="star" name="priority" value="<?php echo $i ?>"> -->
           <option value="<?php echo $i; ?>"><?php echo $i; if($i == 5) echo ' (lowest)'; if($i == 1)echo ' (highest)'; ?></option>             
    <?php } ?>
    </select> 

当我单击调用提交函数的按钮时,有一个选择框,

function add(){   
   var pr = document.getElementById("priority").value;
   console.log(pr);

}

它仅提醒第一个值,即。 5 无论选择哪个选项。

请帮忙。这是一个常见的问题吗?

i am using jquery's thickbox plugin ( ver. 3) and inside it I want to place a form that will be submitted using $.post. But the form elements are not behaving as they should. For eg there is a select box

<select name="priority" id="priority">
    <?php  for($i = 5; $i > 0; $i--){    ?>                                         
           <!-- <input type="radio" class="star" name="priority" value="<?php echo $i ?>"> -->
           <option value="<?php echo $i; ?>"><?php echo $i; if($i == 5) echo ' (lowest)'; if($i == 1)echo ' (highest)'; ?></option>             
    <?php } ?>
    </select> 

when i click on the button that calls the submit function,

function add(){   
   var pr = document.getElementById("priority").value;
   console.log(pr);

}

it alerts only the first value ie. 5 no matter the option selected.

Please help. Is this a commonly faced problem ?

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

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

发布评论

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

评论(3

等往事风中吹 2024-08-21 19:10:58

使用 jQuery:

var pr = $("#priority option:selected").val();

如果你想使用纯 javascript,你应该能够这样做:

var i = document.getElementById("priority").selectedIndex;
var options = document.getElementById("priority").options;

var value = options[i].value;

With jQuery:

var pr = $("#priority option:selected").val();

If you wanted to use pure javascript you should be able to do:

var i = document.getElementById("priority").selectedIndex;
var options = document.getElementById("priority").options;

var value = options[i].value;
最单纯的乌龟 2024-08-21 19:10:58

当您使用 jQuery 时,您应该使用

var pr = $("#priority option:selected").val();

or

var pr = $("#priority").val();

而不是

var pr = document.getElementById("priority").value;

这个应该解决它,

请参阅 http://docs。 jquery.com/Attributes/val 了解更多信息

As you're using jQuery, you should use

var pr = $("#priority option:selected").val();

or

var pr = $("#priority").val();

instead of

var pr = document.getElementById("priority").value;

this should solve it

see http://docs.jquery.com/Attributes/val for more info

南渊 2024-08-21 19:10:58

我具体不了解thickbox,但表单问题对于许多类似Lightbox 的工具来说很常见。原因是该工具 1. 从表单上下文中取出表单元素,并且 2. 通常将它们复制到 DOM 级别的 Thickbox/Lightbox/任何内容中。当框关闭时,元素被销毁或重置,而不是很好地移回到表单中,并且任何更改的设置仍然存在。

通常可以更改此行为,但只能通过更改 *box 脚本本身来安全地来回传输 DOM 元素。

I don't know thickbox specifically, but problems with forms are common to many Lightbox like tools. The reason is that the tool 1. takes the form elements out of the form context and 2. usually copies them into the Thickbox/Lightbox/whatever on DOM level. When the box closes, the element is destroyed or reset instead of moved nicely back into the form with any changed settings surviving.

It is usually possible to alter this behaviour but only by changing the *box script itself so it transfers the DOM elements safely back and forth.

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