简体   繁体   English

单击一个按钮以更改文本输入的文本

[英]Click a button to change the text of text input

I have this part of code in the Codeigniter controller named 'ajaxcalls' : 我在名为'ajaxcalls'的Codeigniter控制器中拥有这部分代码:

public function show_contact_persons($client_id) {
        $data['contact_persons'] = $this->common_model->select_records('ci_contact_persons', 'client_id', $client_id);
        //dump($data);

    function echoArray ($array) {
        foreach ($array as $key => $value)
        {
            if ( true == is_array($value) )
            {
               echoArray($value);
            }
            else
            {
                if ($key == 'contact_person_name') {
                    echo '<input type="button" value="'.$value.'" onclick="changeContact(987)" /><br />';
                }
            }
        }
    }
    echoArray($data);

}

and then the JS part at the page which is calling the PHP file : 然后是调用PHP文件的页面上的JS部分:

function changeContact(contact_name) {
             document.getElementById('contact_person').value = contact_name;
        }

I put the number '987' in this example because the code works only with NUMBERS. 我在此示例中输入数字“ 987”,因为该代码仅适用于NUMBERS。 When I put text inside the bracket, or what I REALLY want to do : 当我将文字放在方括号内或我真正想做的事时:

onclick="changeContact('.$value.')"

then the script doesn't work. 那么该脚本将无法正常工作。 ONLY with numbers. 仅带数字。 What should I change to make it work with strings? 我应该进行哪些更改才能使其与字符串一起使用?

You are very close. 你很亲密

Use this instead : 使用它代替:

onclick="changeContact("'.$value.'")"

Whats happening is the JS is rendering it as changeContact(stringValue); 发生的事情是JS将其呈现为changeContact(stringValue); so its expecting it to be a defined variable. 所以它期望它是一个定义的变量。

Adding the quotations will pass it into the function as a string which will render as : changeContact("stringValue"); 添加引号会将其作为字符串传递给函数,该字符串将呈现为: changeContact("stringValue");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM