Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何显示类型为“”的选定值;电台;在什么地方?_Javascript_Jquery_Xhtml - Fatal编程技术网

Javascript 如何显示类型为“”的选定值;电台;在什么地方?

Javascript 如何显示类型为“”的选定值;电台;在什么地方?,javascript,jquery,xhtml,Javascript,Jquery,Xhtml,单击“填充值”按钮后,按钮下方的div应分别填充首选位置和主位置的选定单选按钮的值 我希望在不使用内联JavaScript的情况下以不引人注目的方式完成它 <div id="form_block"> <div class="home-location"> <ul> <li>Home Location</li> <li> &

单击“填充值”按钮后,按钮下方的div应分别填充首选位置和主位置的选定单选按钮的值

我希望在不使用内联JavaScript的情况下以不引人注目的方式完成它

<div id="form_block">
    <div class="home-location">
        <ul>
            <li>Home Location</li>
            <li>
                <input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
                    for="home-india">India</label></li>
            <li>
                <input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
                    for="home-usa">USA</label></li>
        </ul>
    </div>
    <div class="prefer-location">
        <ul>
            <li>Preferred Location</li>
            <li>
                <input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
                    for="preferred-india">India</label></li>
            <li>
                <input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
                    for="preferred-usa">USA</label></li>
        </ul>
    </div>
    <div class="result">
        My Home Location is: <span class="home-location-result"></span>and my preferred
        location is: <span class="home-location-result"></span>
    </div>
    <input type="submit" value="Populate Values" id="ss" class="submit" />


  • 家乡
  • 印度
  • 美国
  • 首选位置
  • 印度
  • 美国
我的家乡是:和我的首选 地点为:
用这个它对我有用

  <script>
function test()
{
    var home= $('input:radio[name=home-location]:checked').val();
    document.getElementById("h1").innerHTML=home;
    var preferred= $('input:radio[name=home-preferred]:checked').val();
   document.getElementById("h2").innerHTML=preferred;
   return false;

}


</script>
<form onsubmit="return test();">
<div id="form_block">
    <div class="home-location">
        <ul>
            <li>Home Location</li>
            <li>
                <input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
                    for="home-india">India</label></li>
            <li>
                <input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
                    for="home-usa">USA</label></li>
        </ul>
    </div>
    <div class="prefer-location">
        <ul>
            <li>Preferred Location</li>
            <li>
                <input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
                    for="preferred-india">India</label></li>
            <li>
                <input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
                    for="preferred-usa">USA</label></li>
        </ul>
    </div>
    <div class="result">
        My Home Location is: <span class="home-location-result" id="h1"></span>and my preferred
        location is: <span class="home-location-result" id='h2'></span>
    </div>
    <input type="submit" value="Populate Values" id="ss" class="submit" />
</form>

功能测试()
{
var home=$('input:radio[name=home location]:选中').val();
document.getElementById(“h1”).innerHTML=home;
var preferred=$('input:radio[name=home preferred]:选中').val();
document.getElementById(“h2”).innerHTML=首选;
返回false;
}
  • 家乡
  • 印度
  • 美国
  • 首选位置
  • 印度
  • 美国
我的家乡是:和我的首选 地点为:
用这个它对我有用

  <script>
function test()
{
    var home= $('input:radio[name=home-location]:checked').val();
    document.getElementById("h1").innerHTML=home;
    var preferred= $('input:radio[name=home-preferred]:checked').val();
   document.getElementById("h2").innerHTML=preferred;
   return false;

}


</script>
<form onsubmit="return test();">
<div id="form_block">
    <div class="home-location">
        <ul>
            <li>Home Location</li>
            <li>
                <input type="radio" name="home-location" value="india" class="radio" id="home-india" /><label
                    for="home-india">India</label></li>
            <li>
                <input type="radio" name="home-location" value="usa" class="radio" id="home-usa" /><label
                    for="home-usa">USA</label></li>
        </ul>
    </div>
    <div class="prefer-location">
        <ul>
            <li>Preferred Location</li>
            <li>
                <input type="radio" name="home-preferred" value="india" class="radio" id="preferred-india" /><label
                    for="preferred-india">India</label></li>
            <li>
                <input type="radio" name="home-preferred" value="usa" class="radio" id="preferred-usa" /><label
                    for="preferred-usa">USA</label></li>
        </ul>
    </div>
    <div class="result">
        My Home Location is: <span class="home-location-result" id="h1"></span>and my preferred
        location is: <span class="home-location-result" id='h2'></span>
    </div>
    <input type="submit" value="Populate Values" id="ss" class="submit" />
</form>

功能测试()
{
var home=$('input:radio[name=home location]:选中').val();
document.getElementById(“h1”).innerHTML=home;
var preferred=$('input:radio[name=home preferred]:选中').val();
document.getElementById(“h2”).innerHTML=首选;
返回false;
}
  • 家乡
  • 印度
  • 美国
  • 首选位置
  • 印度
  • 美国
我的家乡是:和我的首选 地点为:
修改了您的HTML。为结果范围提供了id属性

$(function(){
    $("#ss").click(function(){
        // Find all input elements with type radio which are descendants of element with id `form`
        var filt = $("#form_block input:radio");
        // Apply a filter to the above object with `name='home-location'` and checked and get the value
        var homeVal = filt.filter("[name='home-location']:checked").val();
        // same as above
        var preferredVal = filt.filter("[name='home-preferred']:checked").val();
        // Set the text of the element width id `home-location-result' with the computed value
        $("#home-location-result").text(homeVal);
        // same as above
        $("#preferred-location-result").text(preferredVal);

        return false;
    });
});

查看一个修改了HTML的文件。为结果范围提供了id属性

$(function(){
    $("#ss").click(function(){
        // Find all input elements with type radio which are descendants of element with id `form`
        var filt = $("#form_block input:radio");
        // Apply a filter to the above object with `name='home-location'` and checked and get the value
        var homeVal = filt.filter("[name='home-location']:checked").val();
        // same as above
        var preferredVal = filt.filter("[name='home-preferred']:checked").val();
        // Set the text of the element width id `home-location-result' with the computed value
        $("#home-location-result").text(homeVal);
        // same as above
        $("#preferred-location-result").text(preferredVal);

        return false;
    });
});

请看a

谢谢Rahul,它正在工作。你能再解释一下这个方法吗,它是如何工作的。这对梅尚·拉胡尔很有用,它在起作用。你能再解释一下这个方法吗,它是如何工作的。会对我有用的