使用 jquery 警报对话框插件,如何将表单中的值发送到回调功能?表格在警报中

发布于 2024-08-31 23:48:24 字数 3049 浏览 0 评论 0原文

使用 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 技术交流群。

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

发布评论

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

评论(1

烏雲後面有陽光 2024-09-07 23:48:24

我自己通过重构代码解决了这个问题。
不管怎样,谢谢你的时间。
结局。

I solved it myself by refactoring my code.
Thanks for your time anyway.
The End.

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