使用 jquery 警报对话框插件,如何将表单中的值发送到回调功能?表格在警报中
使用 jquery 警报对话框插件,如何以表单发送值显示在警报中的回调功能?表单位于警报中,单击“确定”后会发生回调。
这是有效的代码,请忽略此代码,因为它是一个工作示例,它基本上显示了如何序列化表单。此代码取自 .serialise 页面的 Jquery 文档。有问题的代码位于此下方。
<?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?><form id="changecategoryForm2"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select> </form> <p><tt id="results"></tt></p>
<script>
function showValues() {
var str = $("form#changecategoryForm2").serialize();
$("#results").text(str);
}
$(":checkbox, :radio").click(showValues);
$("select").change(showValues);
showValues();
</script>
将该工作代码与我使用下一个代码下方的 Jquery 警报对话框插件所拥有的代码进行比较。 以下是显示警报的默认代码。
jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r) {
if( r ) alert('You entered ' + r);
});
这是我将上面表单的工作 html 代码放入警报中,这是一种称为提示的 JavaScript 警报。
我尝试传递给回调函数的变量是一个序列化表单,其 id 为 changecategoryForm。
- 请注意上面的工作示例 是一种称为 changecategoryForm2
- 下面有问题的形式称为 changecategoryForm
这是有问题的代码。
$(".changecategory").click( function() {
<?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?>
var categoryid = $(this).attr("categoryid");
var itemid = $(this).attr("itemid");
var itemid2 = $(this).attr("itemid");
var itemtitle = $(this).attr("itemtitle");
var parenttag = $(this).parent().get(0).tagName;
var removediv = "itemid_" +itemid;
jAlert(
'Which category do you want to move <b>'+itemtitle+ '</b> to?<br><?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?><form id="changecategoryForm"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select> </form> <p><tt id="results"></tt></p>','Change category', function(r) {
// $(parenttag).fadeOut('slow');
$("#"+removediv).fadeOut('slow');
var newflex = $("#newflex").val();
alert(newflex); // this shows nothing
alert($("#changecategoryForm").serialize());
alert('categoryid ' + categoryid + ' itemid ' + itemid + '.'); //this shows the variables
jAlert(''+ itemtitle +' has been successfully moved.', 'Success');
} // this brace closes function(r)
);
});
Using jquery alerts dialog plugin, how do I send a value in a form that is displayed in an alert, to a callback feature? The form is in the alert, and the callback happens after I click OK.
This is code that works, ignore this code as it is a working example, it basically shows how a form can be serialised. This code is taken from the Jquery documentation from the .serialise page. The problematic code goes below this.
<?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?><form id="changecategoryForm2"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select> </form> <p><tt id="results"></tt></p>
<script>
function showValues() {
var str = $("form#changecategoryForm2").serialize();
$("#results").text(str);
}
$(":checkbox, :radio").click(showValues);
$("select").change(showValues);
showValues();
</script>
Compare that working code to the one I have with the Jquery alerts dialog plugin below the next code.
Below is the default code to show an alert.
jPrompt('Type something:', 'Prefilled value', 'Prompt Dialog', function(r) {
if( r ) alert('You entered ' + r);
});
This is me putting the working html code of the form above into the alert, a type of javascript alert called a prompt.
The variable that I am trying to pass to the callback function is a serialised form called with the id of changecategoryForm.
- Note that the working example above
is a form called
changecategoryForm2 - The problematic form below is called
changecategoryForm
Here is the problematic code.
$(".changecategory").click( function() {
<?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?>
var categoryid = $(this).attr("categoryid");
var itemid = $(this).attr("itemid");
var itemid2 = $(this).attr("itemid");
var itemtitle = $(this).attr("itemtitle");
var parenttag = $(this).parent().get(0).tagName;
var removediv = "itemid_" +itemid;
jAlert(
'Which category do you want to move <b>'+itemtitle+ '</b> to?<br><?php $moveablecats = mysql_query("SELECT * FROM categories WHERE id != '1' "); ?><form id="changecategoryForm"><select id="newflex" name="thecatidichose"> <option value=""></option> <?php while($catrow =mysql_fetch_array($moveablecats)){ ?> <option value="<?php echo "$catrow[id]"; ?>"><?php echo "$catrow[title]"; ?> </option><?php } ?></select> </form> <p><tt id="results"></tt></p>','Change category', function(r) {
// $(parenttag).fadeOut('slow');
$("#"+removediv).fadeOut('slow');
var newflex = $("#newflex").val();
alert(newflex); // this shows nothing
alert($("#changecategoryForm").serialize());
alert('categoryid ' + categoryid + ' itemid ' + itemid + '.'); //this shows the variables
jAlert(''+ itemtitle +' has been successfully moved.', 'Success');
} // this brace closes function(r)
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己通过重构代码解决了这个问题。
不管怎样,谢谢你的时间。
结局。
I solved it myself by refactoring my code.
Thanks for your time anyway.
The End.