在选定的不同单选按钮上调用不同的URL表单JavaScript

在选定的不同单选按钮上调用不同的URL表单JavaScript,javascript,php,html,Javascript,Php,Html,是否有一种方法可以根据选中的单选按钮从java脚本向不同的php URL发送数据 例如: Html 选择html单选按钮 然后仅从JavaScript调用html.php 及 选择css单选按钮 然后只调用CSS.php Html 选择选项: CSS jQuery HTML PHP php脚本 <script type="text/javascript"> $(document).ready(function () { $("#submit").clic

是否有一种方法可以根据选中的单选按钮从java脚本向不同的php URL发送数据

例如:

Html 
选择html单选按钮 然后仅从JavaScript调用html.php

选择css单选按钮

然后只调用CSS.php

Html

选择选项:
CSS
jQuery
HTML
PHP
php脚本

<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").click(function () {
            var datahtml = $('input[type="radio"]:checked').val();
            if ($('input[type="radio"]:checked').length == "0") {
                alert("Select any value");
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "html.php",
                    data: "htmldata=" + htmldata,
                    success: function () {
                        $("#msg").addClass('bg');

                    }
                });
            }
            return false;
        });
    });
</script>

$(文档).ready(函数(){
$(“#提交”)。单击(函数(){
var datahtml=$('input[type=“radio”]:checked').val();
如果($('input[type=“radio”]:checked')。长度==“0”){
警报(“选择任何值”);
}
否则{
$.ajax({
类型:“POST”,
url:“html.php”,
数据:“htmldata=“+htmldata,
成功:函数(){
$(“#msg”).addClass('bg');
}
});
}
返回false;
});
});

事实上,这是一个想法,但我并不是说这是完美的

您所能做的是,对于每个按钮,存储您应该通过单击来访问的url,例如:

<input type="radio" name="user_options" data-url="http://example.org/css" value="css" /> CSS 
<input type="radio" name="user_options" data-url="http://example.org/jquery" value="jquery" /> jQuery 
<input type="radio" name="user_options" data-url="http://example.org/html" value="html" /> HTML
<input type="radio" name="user_options" data-url="http://example.org/php" value="php" /> PHP
然后更换

url: "html.php",

你可能需要修改一些名字,但这里是想法


希望它能帮助您

您可以将
url
中的内容设置为变量,并使用对象文字映射来填充值。您只需确保您考虑了所有可能的情况或提供了一个默认url。否则,url将是未定义的。下面的例子(注意:我还没有测试过它,但这应该给你一个大概的想法)


您可以使用数组/映射来存储根据您单击的收音机应该到达的url。然后,当您单击单选按钮时,您可以检索您应该调用的url并将其设置为ajax调用中的“url”值。您可以分享上述示例吗?这不是
数据:“htmldata=“+htmldata,
就是这
数据:“htmldata=“+datahtml,
但tat应仅在单击按钮时执行,而不是在单选按钮单击时执行。这不是预期的行为吗?在$(“#提交”)之后遇到此代码。单击(函数(){
<input type="radio" name="user_options" data-url="http://example.org/css" value="css" /> CSS 
<input type="radio" name="user_options" data-url="http://example.org/jquery" value="jquery" /> jQuery 
<input type="radio" name="user_options" data-url="http://example.org/html" value="html" /> HTML
<input type="radio" name="user_options" data-url="http://example.org/php" value="php" /> PHP
var remoteUrl=$('input[type="radio"]:checked').data("url");
url: "html.php",
url: remoteUrl,
$(document).ready(function(){
  $("#submit").click(function(){

    // this is your object map. just make sure that you account 
    //for all of the values, otherwise you'll throw an error. 
    var urls = {
      'css': 'css.php', 
      'html': 'html.php'
    };

    var datahtml = $('input[type="radio"]:checked').val();

    if($('input[type="radio"]:checked').length == "0") {
      alert("Select any value");
    } else {
      $.ajax({
        type: "POST",
        url: urls[datahtml], //this is your variable that pulls from the urls object
        data: "htmldata="+htmldata,
        success: function() {
           $("#msg").addClass('bg');
        }
      });
     }
    return false;
  });
});