dtt83024 2015-10-01 13:06
浏览 45
已采纳

用javascript / jquery替换选择输入

I have a page with a form where I have 2 selections, and when you select value X on the first selection, it will change the second selection.
I tried doing this with:

<script language="JavaScript" type="text/JavaScript">
function makeRequestObject() {
    var xmlhttp = false;
    try {
        xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (E) {
            xmlhttp = false;
        }
    }
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        xmlhttp = new XMLHttpRequest();
    }
    return xmlhttp;
}

function replace2(id) {
    var xmlhttp = makeRequestObject();
    var file = 'http://example.com/index.php/result/new_select/'
    xmlhttp.open('GET', file + id, true);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var content = xmlhttp.responseText;
            if (content) {
                document.getElementById('replace').innerHTML = content;
                //$( "div.test" ).replaceWith(content);
            }
        }
    }
    xmlhttp.send(null)
}
</script>

And in the form I have this:

<th id="replace">
    <?php
    $fruit_options = array();
    $fruit_options[-1] = 'Select ALL';
    foreach ($tests as $fruit) {
        $fruit_options[$fruit->ID] = $fruit->Nume;
    }
    echo form_dropdown('test', $fruit_options, $fruit->ID, $fruit->Nume);
    ?>
</th>

Everything works, when I select the first dropdown, the second selection will be modified (I call it with: echo form_dropdown('test1', $fruit_options, -1, 'onchange="replace2(this.value)"');)

But when I press "Submit", the controller will not see the value of post test.

In result/new_select/id I have something like this:

<?php
$fruit_options = array();
$fruit_options[-1] = 'Select ALL';
foreach ($tests as $fruit) {
    $fruit_options[$fruit->ID] = $fruit->Nume;
}
echo form_dropdown('test', $fruit_options, $fruit->ID, $fruit->Nume);
?>

Does anybody know why my code doesn't work, or what a better way to do this would be?

LE

        <form action="http://example.com/index.php/test/query" method="post" accept-charset="utf-8">    
<table>                    
        <th>
            <select name="test1" onchange="replace2(this.value)">
                <option value="-1" selected="selected">Select ALL</option>
                <option value="515">515</option>
                <option value="252">252</option>
                <option value="327">327</option>
                <option value="409">409</option>
            </select>                        
        </th>
        <th id="replace">
            <select name="test" 315>
                <option value="-1">Select ALL</option>
                <option value="1">1</option>
                <option value="3">3</option>
                <option value="4">4</option>
                <option value="5">5</option>
                <option value="6">6</option>
                <option value="315" selected="selected">315</option>
            </select>                        
        </th>
        <th>
            <input type="submit" name="mysubmit" value="Search!"  />                            
        </th>
</table>
    </form>  

In controller i see value for test1 but i can't see value of test.

  • 写回答

1条回答 默认 最新

  • doulan0297 2015-10-01 14:03
    关注

    The problem here seems to be your table structure. You are forgetting that tables need rows <tr> and columns <td>, therefore when JS appends any HTML, it places them outside the form thus your form request not sending the right POST info.

    Restructure your table like this:

    <form method="post">
        <table>
            <tr>
                <td>
                    <?php
                    $fruit_options = array();
                    $fruit_options[-1] = 'Select ALL';
                    foreach ($tests as $fruit) {
                        $fruit_options[$fruit->ID] = $fruit->Nume;
                    }
                    echo form_dropdown('test1', $fruit_options, -1, 'onchange="replace2(this.value)"');
                    ?>
                </td>
            </tr>
            <tr>
                <td id="replace">
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit"/>
                </td>
            </tr>
        </table>
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。