dqyy38265 2011-07-25 18:34
浏览 59
已采纳

如何使用PEAR的HTML_Quickform包动态添加表单元素

How do I dynamically add form elements using HTML_Quickform. I know how this is done using plain HTML form elements and using Javascript (see code below). I wanted to know if this is easy to do using HTML_Quickform. I am still looking at this site: http://devzone.zend.com/article/2699-Generating-and-Validating-Web-Forms-With-PEAR-HTML_QuickForm. HTML_Quickform lets you easily add validation rules. I began creating a similar HTML_Quickform code - but I could not figure out how to dynamically add form elements using HTML_Quickform:

// Our Default Form Options
$opts = array('size' => 20, 'maxlength' => 255, 'id' => 'name1', 'class' => 'clonedInput' );

$form->addElement('text', 'first_name', 'First Name', $opts);
$form->addElement('button','btnAdd', 'Add another name', array('id' => 'btnAdd'));
$form->addElement('button','btnDel', 'Remove Name', array('id' => 'btnDel') );

// Submit button
$form->addElement('image', 'register', 'images/continue.gif');

// Define filters and validation rules
$form->applyFilter('first_name', 'trim');
$form->addRule('first_name', 'Please enter your First Name', 'required', null, 'client');

$formsource = $form->toHtml();

echo $formsource; 
?>

The code for the plain HTML with Javascript is as follows:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    $('#btnAdd').click(function() {
        var num     = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
        var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

        // create the new element via clone(), and manipulate it's ID using newNum value
        var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

        // manipulate the name/id values of the input inside the new element
        newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);

        // insert the new element after the last "duplicatable" input field
        $('#input' + num).after(newElem);

        // enable the "remove" button
        $('#btnDel').attr('disabled','');

        // business rule: you can only add 5 names
        if (newNum == 5)
            $('#btnAdd').attr('disabled','disabled');
    });

    $('#btnDel').click(function() {
        var num = $('.clonedInput').length; // how many "duplicatable" input fields we currently have
        $('#input' + num).remove();     // remove the last element

        // enable the "add" button
        $('#btnAdd').attr('disabled','');

        // if only one element remains, disable the "remove" button
        if (num-1 == 1)
            $('#btnDel').attr('disabled','disabled');
    });

    $('#btnDel').attr('disabled','disabled');
});
</script>


<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Name: <input type="text" name="name1" id="name1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
        <input type="button" id="btnDel" value="remove name" />
    </div>
</form>
  • 写回答

1条回答 默认 最新

  • duanjiu1894 2011-07-25 20:14
    关注

    Unless I'm misunderstanding your question, you can't. You're using HTML_Quickform to generate HTML on the server. Once you output $formsource, your php code (and thus HTML_Quickform) cease to execute, so you wouldn't be able to programmatically add anything with HTML_Quickform. So, once you output the HTML to the browser, you can only manipulate your HTML-based form with JavaScript.

    One workaround would be to create some HTML form fragments with HTML_Quickform and inject those into your HTML form with AJAX.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度