猿问

如何使用带有 id 参数的 js 函数交换 html 按钮内的文本?

实际上 Laravel 并不知道模型在哪里


如果这个例子有效:


public function user()

{

    return $this->belongsTo(User::class);

}

这是因为模型可能位于同一文件夹或命名空间中,否则您可能应该像这样从其他命名空间导入所需的模型。



//At the top of the file you will import the class


use Maybe\Another\Folder\Namespace\OtherObject;


public function user()

{

    return $this->belongsTo(OtherObject::class);

}

如果您不想“导入”对象,您应该像这样使用类的完整路径。


public function user()

{

    return $this->belongsTo(App\OtherFolder\OtherObject::class);

}

总之,laravel 不知道在哪里查找类定义,但是如果您在参数中传递一个实例,该实例将为服务容器解析,但这是另一个与模型绑定更相关的主题


public function method(MyObject $instance)

{

    //Laravel will try to automatically generate an instance of $instance

}


慕丝7291255
浏览 176回答 3
3回答

桃花长相依

您没有得到正确的输出,因为您在参数中传递了错误的 id。在输入中,您将 id 指定为“ .$id. ”,但在参数中,您将 id 传递为“ $.id. ”。提供正确的 id,您将获得预期的输出。function swaptxt(index, text1, text2){&nbsp; var elem = document.getElementById(index);&nbsp; if (elem.value===text1)&nbsp; &nbsp; elem.value = text2;&nbsp; else&nbsp; &nbsp; elem.value = text1;}<input type='button' id=".$id." onclick='swaptxt(".$id.",".$value.", ".$var1.")' value=".$value">

慕慕森

不要从 PHP 回显它,只是退出 PHP,只使用 PHP 来添加变量。?> <!-- we are no longer in PHP --><input type='button' id='<?php echo $id;&nbsp; &nbsp; ?>'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;onclick='swaptxt('<?php echo $id;&nbsp; &nbsp; ?>',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<?php echo $var1;&nbsp; ?>',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '<?php echo $value; ?>')'&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;value='<?php echo $value; ?>'><?php // back to PHP now

牧羊人nacy

连接它的另一种方法:onclick="swaptxt(\''&nbsp;.&nbsp;$id&nbsp;.&nbsp;'\',\''&nbsp;.&nbsp;$var1&nbsp;.&nbsp;'\',\''&nbsp;.&nbsp;$value&nbsp;.'\');"
随时随地看视频慕课网APP
我要回答