开发者

Forced selection in hidden / Backend using Javascript OR JQuery

I have regular form like this.

    <form action="/domainchecker.php" method="post">    
    <input type="text" name="domain" size="20">

    <fieldset>
    <select name="ext">
    <option>.com</option>
    <option>.net</option>
    <option>.org</option>
    <option>.us</option>
    <option>.i开发者_StackOverflow中文版nfo</option>
    <option>.biz</option>
    <option>.mobi</option>
    <option>.name</option>
    <option>.tv</option>
    <option>.me</option>        
    </select>
    </fieldset>

    <input type="submit" value="Go">
    </form>

This selectbox enables to select only one option.

What I want is Forced selection in hidden / backend for the first 5 options regardless user select it or not.

How can I achieve this using JavaScript OR JQuery ?

Thanks.


Since you are using PHP you can try this:

<form action="/domainchecker.php" method="post">    
<input type="hidden" name="ext[]" value=".com">
<input type="hidden" name="ext[]" value=".net">
<input type="hidden" name="ext[]" value=".org">
<input type="hidden" name="ext[]" value=".us">
<input type="hidden" name="ext[]" value=".info">
<input type="text" name="domain" size="20">

<fieldset>
<select name="ext[]">
<option>.com</option>
<option>.net</option>
<option>.org</option>
<option>.us</option>
<option>.info</option>
<option>.biz</option>
<option>.mobi</option>
<option>.name</option>
<option>.tv</option>
<option>.me</option>        
</select>
</fieldset>

<input type="submit" value="Go">
</form>

Note the brackets in the fields' names. In your PHP script you'll need something like:

$extensions = $_POST['ext'];
foreach ($extensions as $ext) {
    #do something
}

Note that the $extensions array will contain duplicates if the user selected one of the first 5 entries in the dropdown.

You can also use checkboxes instead of a dropdown like this:

<form action="/domainchecker.php" method="post">    
<input type="text" name="domain" size="20">

<fieldset>
<input type="checkbox" name="ext[]" value=".com" selected="true">
<input type="checkbox" name="ext[]" value=".net" selected="true">
<input type="checkbox" name="ext[]" value=".org" selected="true">
<input type="checkbox" name="ext[]" value=".us" selected="true">
<input type="checkbox" name="ext[]" value=".info" selected="true">
<input type="checkbox" name="ext[]" value=".biz">
<input type="checkbox" name="ext[]" value=".mobi">
<input type="checkbox" name="ext[]" value=".name">
<input type="checkbox" name="ext[]" value=".tv">
<input type="checkbox" name="ext[]" value=".me">
</select>
</fieldset>

<input type="submit" value="Go">
</form>

The PHP is the same, without the duplicate issue


Add them server-side.


you can't do multiple selection on your select if attribute multiple is not set

like this, then have default selection by setting the option's attribute selected="selected"

<fieldset>
    <select name="ext" size="5" multiple="multiple" >
        <option>.com</option>
        <option>.net</option>
        <option selected="selected">.org</option>
        <option selected="selected">.us</option>
        <option selected="selected">.info</option>
        <option>.biz</option>
        <option selected="selected">.mobi</option>
        <option>.name</option>
        <option>.tv</option>
        <option>.me</option>        
    </select>
</fieldset>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜