根据下拉选择更改表单目标

时间:2015-06-07 14:36:10

标签: javascript jquery forms target

我希望每个下拉选项都有一个“_blank”目标,但选项4除外。如何根据表单中选择的选项切换目标?

    <form action="mypage.php" method="post" target="_blank" />
<input type="hidden" name="turnaround" value="20">
<table>
    <tr>
        <td>Platform</td>
        <td>
            <select required id="cu" name="plat" style="min-width: 120px;">
                <option value="1">1</option>
                <option value="2">2 (Desktop Only)</option>
                <option value="3">3 (Mobile Only)</option>
                <option value="4">4 (Wildcard)</option>
                <option value="5">Catch-All</option>        
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>
<div><input type="submit" value="Select Platform" /></div>
</form>

3 个答案:

答案 0 :(得分:0)

给你的表单一个id:

<form action="mypage.php" id="myForm" method="post" target="_blank" />
<input type="hidden" name="turnaround" value="20">
<table>
    <tr>
        <td>Platform</td>
        <td>
            <select required id="cu" name="plat" style="min-width: 120px;">
                <option value="1">1</option>
                <option value="2">2 (Desktop Only)</option>
                <option value="3">3 (Mobile Only)</option>
                <option value="4">4 (Wildcard)</option>
                <option value="5">Catch-All</option>        
            </select>
        </td>
    </tr>
    <tr>
        <td colspan="2">&nbsp;</td>
    </tr>
</table>

你的js:

$('#myForm').attr("target") = $('#cu').val() == "4" ? "" : "_blank";

未经测试但应该可以使用

答案 1 :(得分:0)

可能是你想给class或id提交按钮。

$('input[type=submit]').click(function(e){
    if($('#cu').val() == '4'){
        $(this).closest('form').prop('target','');    
    }  
});

答案 2 :(得分:0)

选中此fiddle

JS:

$("#cu").on("change",function(){
    var a = $(this).val();
    if(a == "4"){
         $("#formId").removeAttr("target")
    }else{
    $("#formId").attr("target","_blank")
    }

})